package cn.sdormitory.controller.report;

import cn.sdormitory.common.api.CommonPage;
import cn.sdormitory.common.api.CommonResult;
import cn.sdormitory.report.req.RetentionStatisticsReq;
import cn.sdormitory.report.service.RetentionStatisticsService;
import cn.sdormitory.report.vo.RetentionStatisticsVO;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author : yy
 * @date: 2025/4/12 8:30
 * @description: 滞留统计
 */
@RestController
@Api(tags = "滞留统计")
@RequestMapping("/report/retentionStatistics")
public class RetentionStatisticsController {
    @Resource
    private RetentionStatisticsService retentionStatisticsService;

    @ApiOperation("查询滞留统计列表")
    @PostMapping(value = "/list")
    public CommonResult<CommonPage<RetentionStatisticsVO>> list(@RequestBody RetentionStatisticsReq statisticsReq) {
        IPage<RetentionStatisticsVO> page = retentionStatisticsService.list(statisticsReq);
        return CommonResult.success(CommonPage.restPage(page));
    }

    @ApiOperation("导出滞留统计列表")
    @PostMapping(value = "/export")
    public CommonResult export(HttpServletResponse response,@RequestBody RetentionStatisticsReq statisticsReq) {
        List<RetentionStatisticsVO> retentionStatisticsVOS = retentionStatisticsService.export(statisticsReq);
        try {
            EasyExcel.write(response.getOutputStream(), RetentionStatisticsVO.class).sheet("滞留统计").doWrite(retentionStatisticsVOS);
            return CommonResult.success();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return CommonResult.failed();

    }
}
